package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.*;
import com.genexususa.eo.bing.data.GxItemException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GxGoal extends Goal {
    private long gxId;

    public GxGoal(Element element) throws IllegalArgumentException, GxItemException {
        NodeList costModelsNode = element.getElementsByTagName("CostModelCollection");
        if (costModelsNode.getLength() > 0) {
            NodeList costModelNode = ((Element)costModelsNode.item(0)).getElementsByTagName("CostModel");
            String[] costModels = new String[costModelNode.getLength()];
            for (int i=0; i<costModelNode.getLength(); i++) {
                if (costModelNode.item(i).getChildNodes().getLength() > 0) {
                    costModels[i] = costModelNode.item(i).getChildNodes().item(0).getNodeValue();
                }
            }
            this.setCostModel(costModels);
        }
        if (element.getElementsByTagName("DaysApplicableForConversion").getLength() > 0 &&
            element.getElementsByTagName("DaysApplicableForConversion").item(0).getChildNodes().getLength() > 0) {
            this.setDaysApplicableForConversion(DaysApplicableForConversion.fromString(((Node) element.getElementsByTagName("DaysApplicableForConversion").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("GxGoalId").getLength() > 0 &&
            element.getElementsByTagName("GxGoalId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxGoalId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxId = Long.valueOf(((Node)element.getElementsByTagName("GxGoalId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external Goal id.");
        }
        if (element.getElementsByTagName("BingGoalId").getLength() > 0 &&
            element.getElementsByTagName("BingGoalId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingGoalId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setId(Long.valueOf(((Node) element.getElementsByTagName("BingGoalId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("Name").getLength() > 0 &&
            element.getElementsByTagName("Name").item(0).getChildNodes().getLength() > 0) {
            this.setName(((Node)element.getElementsByTagName("Name").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("RevenueModel").getLength() > 0 ) {
            this.setRevenueModel(new GxRevenueModel((Element)element.getElementsByTagName("RevenueModel").item(0)));
        }
        NodeList stepsNode = element.getElementsByTagName("StepCollection");
        if (stepsNode.getLength() > 0) {
            NodeList stepNode = ((Element)stepsNode.item(0)).getElementsByTagName("Step");
            Step[] steps = new Step[stepNode.getLength()];
            for (int temp = 0; temp < stepNode.getLength(); temp++) {
                steps[temp] = new GxStep((Element)stepNode.item(temp));
            }
            this.setSteps(steps);
        }
        if (element.getElementsByTagName("YEventId").getLength() > 0 &&
            element.getElementsByTagName("YEventId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("YEventId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setYEventId(Integer.parseInt(((Node) element.getElementsByTagName("YEventId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
    }

    public long getGxId() {
        return gxId;
    }

    public void setGxId(long gxId) {
        this.gxId = gxId;
    }
}
